<?php
/**
 * Created by PhpStorm.
 * User: Sergey
 * Date: 23.03.14
 * Time: 2:20
 */

include_once BASE_DQ_PLUGIN_PATH . "classes/Config.php";
include_once BASE_DQ_PLUGIN_PATH . "classes/ConnectionFactory.php";
include_once BASE_DQ_PLUGIN_PATH . "classes/Result.php";

class ResultDao extends ConnectionFactory
{
    /**
     * @param $row
     * @return null|Result
     */
    public function fetch($row)
    {
        try {
            $result = new Result();
            $result->quizId = $row->quiz_id;
            $result->questionId = $row->question_id;
            $result->answers = explode(",", $row->answers);
            $result->userId = $row->user_id;
            return $result;
        } catch (Exception $ex) {
            return null;
        }
    }

    /**
     * @return array
     */
    public function getAll()
    {
        $result = $this->getAllRows(Config::get()->getResultsTableName());
        $results = array();
        foreach ($result as $row) {
            $results[] = $this->fetch($row);
        }
        return $results;
    }

    /**
     * @param int $quizId
     * @param int $userId
     * @return array
     */
    public function getAllByQuizUser($quizId, $userId)
    {
        $table = Config::get()->getResultsTableName();
        $query = "
        SELECT *
        FROM $table
        WHERE quiz_id=$quizId AND user_id=$userId";

        global $wpdb;
        $result = $wpdb->get_results($query);

        $results = array();
        foreach ($result as $row) {
            $results[] = $this->fetch($row);
        }
        return $results;
    }

    /**
     * @param int $quizId
     * @param int $questionId
     * @param int $userId
     * @return null|Result
     */
    public function getByQuizQuestionUser($quizId, $questionId, $userId)
    {
        $table = Config::get()->getResultsTableName();
        $query = "
        SELECT *
        FROM $table
        WHERE
          quiz_id=$quizId AND
          question_id=$questionId AND
          user_id=$userId";

        global $wpdb;
        $result = $wpdb->get_results($query);

        return count($result) > 0 ? $this->fetch($result[0]) : null;
    }

    /**
     * @param Result $result
     */
    public function add(Result $result)
    {
        global $wpdb;
        $wpdb->insert(
            Config::get()->getResultsTableName(),
            array(
                "quiz_id" => $result->quizId,
                "question_id" => $result->questionId,
                "answers" => implode(",", $result->answers),
                "user_id" => $result->userId,
            ),
            array("%d", "%d", "%s", "%d")
        );
    }

    /**
     * @param int $quizId
     * @param int $userId
     */
    public function remove($quizId, $userId)
    {
        $tableName = Config::get()->getResultsTableName();
        global $wpdb;
        $wpdb->delete(
            $tableName,
            array(
                "quiz_id" => $quizId,
                "user_id" => $userId
            ),
            array("%d", "%d")
        );
    }
} 